
package guipk;
import commonpk.Addable;
import substancepk.Player;
import commonpk.DBOperation;
import java.sql.SQLException;
import javax.swing.DefaultListModel;

/**
 *
 * @author LJC
 */
public class AdFrame extends javax.swing.JFrame {
    
    // 接口
    Addable addable;
    
    //************************************************************
    /* 成员 */
    private DBOperation         DBO;               // 数据库操作
    private DefaultListModel    defaultListModel;  // 列表Model
    private Player              tempPlayer;        // 当前玩家
    private String              playerName;        // 玩家名
    private String              playerId;          // 玩家账号
    private String              playerGrade;       // 玩家等级
    private String              playerScore;       // 玩家积分
    //************************************************************

    
    
    
    //*****************************************************
    /* 构造器 */
    
    public AdFrame() {
        // 配置窗口
        initComponents();
        this.setLocationRelativeTo(null);
        this.setVisible(true);
        // 构造成员
        DBO = new DBOperation();
        defaultListModel = new DefaultListModel<>();
        this.resultList.setModel(defaultListModel);
    }
    
    //*****************************************************
    
    
    
    //*****************************************************
    /* 其他方法 */
    
    
    // 从窗口组件生成对象/////////////////////////////////////////////////////
    public Player getPlayerInstance(){
        Player player = new Player();
        player.setPlayerName(this.PlayerNameTextField.getText());
        player.setPlayerId(Integer.parseInt(this.PlayerIdTextField.getText()));
        player.setPlayerGrade(Integer.parseInt(this.PlayerGradeComboBox.getSelectedItem().toString()));
        player.setPlayerScore(Integer.parseInt(this.PlayerScoreTextField.getText()));
        player.setPlayerPwd("111111");
        return player;
    }
    
    // 显示对象信息
    public void showPlayerInfo(){
        this.PlayerNameTextField.setText(this.tempPlayer.getPlayerName());
        this.PlayerIdTextField.setText(String.valueOf(this.tempPlayer.getPlayerId()));
        this.PlayerGradeComboBox.setSelectedItem(String.valueOf(this.tempPlayer.getPlayerGrade()));
        this.PlayerScoreTextField.setText(String.valueOf(this.tempPlayer.getPlayerScore()));
    }
    
    
    
    
    //*****************************************************
  
    
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        PlayerNameLabel = new javax.swing.JLabel();
        PlayerIdLabel = new javax.swing.JLabel();
        PlayerNameTextField = new javax.swing.JTextField();
        PlayerIdTextField = new javax.swing.JTextField();
        PlayerGradeLabel = new javax.swing.JLabel();
        PlayerGradeComboBox = new javax.swing.JComboBox<>();
        AddPlayerButton = new javax.swing.JButton();
        DelPlayerButton = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        resultList = new javax.swing.JList<>();
        jLabel4 = new javax.swing.JLabel();
        PlayerScoreTextField = new javax.swing.JTextField();
        SearchPlayerButton = new javax.swing.JButton();
        EditPlayerButton = new javax.swing.JButton();
        validCheckBox = new javax.swing.JCheckBox();
        tipLabel = new javax.swing.JLabel();
        ReturnButton = new javax.swing.JButton();
        clearButton = new javax.swing.JButton();

        jLabel1.setText("jLabel1");

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("玩家管理");
        setBounds(new java.awt.Rectangle(0, 0, 288, 298));
        setResizable(false);

        jPanel1.setBackground(new java.awt.Color(204, 204, 204));
        jPanel1.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        PlayerNameLabel.setFont(new java.awt.Font("宋体", 1, 12)); // NOI18N
        PlayerNameLabel.setText("玩家名");
        jPanel1.add(PlayerNameLabel, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 20, -1, 20));

        PlayerIdLabel.setFont(new java.awt.Font("宋体", 1, 12)); // NOI18N
        PlayerIdLabel.setText("玩家序号");
        jPanel1.add(PlayerIdLabel, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 50, -1, 20));
        jPanel1.add(PlayerNameTextField, new org.netbeans.lib.awtextra.AbsoluteConstraints(70, 20, 90, -1));
        jPanel1.add(PlayerIdTextField, new org.netbeans.lib.awtextra.AbsoluteConstraints(70, 50, 90, -1));

        PlayerGradeLabel.setFont(new java.awt.Font("宋体", 1, 12)); // NOI18N
        PlayerGradeLabel.setText("玩家等级");
        jPanel1.add(PlayerGradeLabel, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 80, -1, 20));

        PlayerGradeComboBox.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "1", "2", "3", "4", "5" }));
        jPanel1.add(PlayerGradeComboBox, new org.netbeans.lib.awtextra.AbsoluteConstraints(70, 80, 50, -1));

        AddPlayerButton.setText("注册");
        AddPlayerButton.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                AddPlayerButtonMouseEntered(evt);
            }
        });
        AddPlayerButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                AddPlayerButtonActionPerformed(evt);
            }
        });
        jPanel1.add(AddPlayerButton, new org.netbeans.lib.awtextra.AbsoluteConstraints(210, 20, -1, -1));

        DelPlayerButton.setText("注销");
        DelPlayerButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                DelPlayerButtonActionPerformed(evt);
            }
        });
        jPanel1.add(DelPlayerButton, new org.netbeans.lib.awtextra.AbsoluteConstraints(210, 50, -1, -1));

        resultList.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                resultListValueChanged(evt);
            }
        });
        jScrollPane1.setViewportView(resultList);

        jPanel1.add(jScrollPane1, new org.netbeans.lib.awtextra.AbsoluteConstraints(30, 170, 220, 90));

        jLabel4.setFont(new java.awt.Font("宋体", 1, 12)); // NOI18N
        jLabel4.setText("玩家得分");
        jPanel1.add(jLabel4, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 110, -1, -1));
        jPanel1.add(PlayerScoreTextField, new org.netbeans.lib.awtextra.AbsoluteConstraints(70, 110, 50, -1));

        SearchPlayerButton.setText("查找");
        SearchPlayerButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                SearchPlayerButtonActionPerformed(evt);
            }
        });
        jPanel1.add(SearchPlayerButton, new org.netbeans.lib.awtextra.AbsoluteConstraints(210, 80, -1, -1));

        EditPlayerButton.setText("修改");
        EditPlayerButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                EditPlayerButtonActionPerformed(evt);
            }
        });
        jPanel1.add(EditPlayerButton, new org.netbeans.lib.awtextra.AbsoluteConstraints(210, 110, -1, -1));

        validCheckBox.setBackground(new java.awt.Color(204, 204, 204));
        validCheckBox.setSelected(true);
        validCheckBox.setText("可用");
        jPanel1.add(validCheckBox, new org.netbeans.lib.awtextra.AbsoluteConstraints(130, 90, 60, 30));

        tipLabel.setFont(new java.awt.Font("宋体", 1, 12)); // NOI18N
        tipLabel.setForeground(new java.awt.Color(255, 0, 0));
        jPanel1.add(tipLabel, new org.netbeans.lib.awtextra.AbsoluteConstraints(70, 150, 150, 20));

        ReturnButton.setText("返回");
        ReturnButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ReturnButtonActionPerformed(evt);
            }
        });
        jPanel1.add(ReturnButton, new org.netbeans.lib.awtextra.AbsoluteConstraints(190, 280, -1, -1));

        clearButton.setText("清空");
        clearButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                clearButtonActionPerformed(evt);
            }
        });
        jPanel1.add(clearButton, new org.netbeans.lib.awtextra.AbsoluteConstraints(30, 280, -1, -1));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 288, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 0, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 321, javax.swing.GroupLayout.PREFERRED_SIZE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void DelPlayerButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_DelPlayerButtonActionPerformed
        // TODO add your handling code here:
        
        // 获取当前对象
        int currentIndex = this.resultList.getSelectedIndex();
        tempPlayer = (Player)this.defaultListModel.getElementAt(currentIndex);
        
        // 生成 删除语句 (对 玩家名 删除)
        String sql = "delete from PlayerTable where PlayerName='" + this.tempPlayer.getPlayerName() + "'";
        
        // 执行 删除语句
        this.DBO.DBUpdate(sql);
        
        // 更新Model
        this.resultList.clearSelection();
        this.defaultListModel.removeElementAt(currentIndex);
        
        // 完成提示
        this.tipLabel.setText("注销完成");
        
    }//GEN-LAST:event_DelPlayerButtonActionPerformed

    private void AddPlayerButtonMouseEntered(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_AddPlayerButtonMouseEntered
        // TODO add your handling code here:
        
    }//GEN-LAST:event_AddPlayerButtonMouseEntered

    private void ReturnButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ReturnButtonActionPerformed
        // TODO add your handling code here:
        new AdGoFrame();
        this.dispose();
    }//GEN-LAST:event_ReturnButtonActionPerformed

    private void AddPlayerButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_AddPlayerButtonActionPerformed
        // TODO add your handling code here:
        
        // 提取窗口中的信息 生成当前对象
        tempPlayer = getPlayerInstance();
        
        // 生成写入语句
        String sql = "insert into PlayerTable values("
                + this.tempPlayer.getPlayerId() + ",'"
                + this.tempPlayer.getPlayerPwd() + "',"
                + this.tempPlayer.getPlayerGrade() + ","
                + this.tempPlayer.getPlayerScore() + ",'"
                + this.tempPlayer.getPlayerName() + "')";
        
        // 执行 写入语句
        this.DBO.DBUpdate(sql);
        
        // 更新Model
        this.defaultListModel.addElement(this.tempPlayer);
        
        // 完成提示
        this.tipLabel.setText("注册完成");
        
    }//GEN-LAST:event_AddPlayerButtonActionPerformed

    private void SearchPlayerButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_SearchPlayerButtonActionPerformed
        // TODO add your handling code here:
        
        // 声明查找语句
        String sql = "";
        
         // 生成查找语句
        sql = "select * from PlayerTable";
            
        // 执行 查找语句
        this.DBO.DBQuary(sql);
            
        if(this.PlayerNameTextField.getText().equals("")){
            
            // 清空列表
            this.defaultListModel.removeAllElements();
            
            // 插入列表
            try{
                
                while(this.DBO.getRs().next()){
                    
                    // 从 结果集 生成当前对象
                    this.tempPlayer = new Player();
                    tempPlayer.setPlayerName(this.DBO.getRs().getString("PlayerName"));
                    tempPlayer.setPlayerId(this.DBO.getRs().getInt("PlayerId"));
                    tempPlayer.setPlayerGrade(Integer.parseInt(this.DBO.getRs().getString("PlayerGrade")));
                    tempPlayer.setPlayerScore(this.DBO.getRs().getInt("PlayerScore"));
                    
                    // 将当前对象插入Model
                    this.defaultListModel.addElement(tempPlayer);
                    
                }
                
            }catch(SQLException e){
                e.printStackTrace();
            }
            
        }else{
            
            // 清空列表
            this.defaultListModel.removeAllElements();
            
            try{
                
                while(this.DBO.getRs().next()){
                    if(this.PlayerNameTextField.getText().equals(this.DBO.getRs().getString("PlayerName"))){
                        
                        //从 数据表 生成对象
                        tempPlayer = new Player();
                        tempPlayer.setPlayerName(this.DBO.getRs().getString("PlayerName"));
                        tempPlayer.setPlayerId(this.DBO.getRs().getInt("PlayerId"));
                        tempPlayer.setPlayerGrade(this.DBO.getRs().getInt("PlayerGrade"));
                        tempPlayer.setPlayerScore(this.DBO.getRs().getInt("PlayerScore"));
                        
                        
                        // 将对象插入Model
                        this.defaultListModel.addElement(tempPlayer);
                        
                        // 显示当前对象信息
                        showPlayerInfo();
                        
                    }
                }
                
            }catch(SQLException e){
                e.printStackTrace();
            }
            
        }
    }//GEN-LAST:event_SearchPlayerButtonActionPerformed

    private void resultListValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_resultListValueChanged
        // TODO add your handling code here:
        
        
        if((!this.defaultListModel.isEmpty()) && (!this.resultList.isSelectionEmpty())){
            int currentIndex = this.resultList.getSelectedIndex();
            tempPlayer = (Player)this.defaultListModel.getElementAt(currentIndex);
            showPlayerInfo();
        }else{
            
        }
    }//GEN-LAST:event_resultListValueChanged

    private void clearButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_clearButtonActionPerformed
        // TODO add your handling code here:
        this.PlayerNameTextField.setText("");
        this.PlayerIdTextField.setText("");
        this.PlayerGradeComboBox.setSelectedItem("1");
        this.PlayerScoreTextField.setText("");
        this.defaultListModel.removeAllElements();
    }//GEN-LAST:event_clearButtonActionPerformed

    private void EditPlayerButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_EditPlayerButtonActionPerformed
        // TODO add your handling code here:
        int currentIndex = this.resultList.getSelectedIndex();
        tempPlayer = getPlayerInstance();
        
        // 生成 更新语句
        String sql = "update PlayerTable set PlayerName='" + this.tempPlayer.getPlayerName()
                + "',PlayerGrade=" + this.tempPlayer.getPlayerGrade()
                + ",PlayerScore=" + this.tempPlayer.getPlayerScore()
                + " where PlayerId=" + this.tempPlayer.getPlayerId();
        
        // 执行 更新语句
        this.DBO.DBUpdate(sql);
        
        // 更新Model
        this.defaultListModel.setElementAt(tempPlayer, currentIndex);
        
        // 完成提示
        this.tipLabel.setText("修改完成");
    }//GEN-LAST:event_EditPlayerButtonActionPerformed

    
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(AdFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(AdFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(AdFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(AdFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new AdFrame().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton AddPlayerButton;
    private javax.swing.JButton DelPlayerButton;
    private javax.swing.JButton EditPlayerButton;
    private javax.swing.JComboBox<String> PlayerGradeComboBox;
    private javax.swing.JLabel PlayerGradeLabel;
    private javax.swing.JLabel PlayerIdLabel;
    private javax.swing.JTextField PlayerIdTextField;
    private javax.swing.JLabel PlayerNameLabel;
    private javax.swing.JTextField PlayerNameTextField;
    private javax.swing.JTextField PlayerScoreTextField;
    private javax.swing.JButton ReturnButton;
    private javax.swing.JButton SearchPlayerButton;
    private javax.swing.JButton clearButton;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JList<String> resultList;
    private javax.swing.JLabel tipLabel;
    private javax.swing.JCheckBox validCheckBox;
    // End of variables declaration//GEN-END:variables
}
